package top.ikunikun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import top.ikunikun.common.CustomException;
import top.ikunikun.common.Result;
import top.ikunikun.domain.Category;
import top.ikunikun.domain.Dish;
import top.ikunikun.domain.Setmeal;
import top.ikunikun.mapper.CategoryMapper;
import top.ikunikun.service.CategoryService;
import top.ikunikun.service.DishService;
import top.ikunikun.service.SetmealService;

import java.io.Serializable;
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;
    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件，根据分类id进行查询
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        if(count1 > 0){
            // 已经关联菜品，抛出一个业务异常
            throw new CustomException("当前分类关联了菜品，不能删除");
        }
        // 查询当前分类是否关联了套餐，若已关联，抛出异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件，根据分类id经i选哪个查询
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);
        if(count2 > 0){
            // 分类已经关联套餐，抛出一个业务异常
            throw new CustomException("当前分类关联了套餐，不能删除");
        }
        super.removeById(id);
    }
}
